<?php

// +-----------------------------------------------------
// | Name: 星空框架  后台菜单模型
// +-----------------------------------------------------
// | Version: V5.0
// +-----------------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +-----------------------------------------------------
// | Time: 2021-04-19 11:14
// +-----------------------------------------------------
declare(strict_types=1);

namespace app\common\model;

use think\model\concern\SoftDelete;

/**
 * @xk 后台菜单模型
 */
class Menu extends BaseModel
{
    use SoftDelete;
    protected $name = 'menu';
    protected $pk = 'menu_id';
    protected $deleteTime = 'delete_time';

    /**
     * @xk 新增前操作
     *
     * @method onBeforeInsert
     *
     * @param [type] $data [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-19T11:16:49+080
     */
    public static function onBeforeInsert($data)
    {
    }

    /**
     * @xk 更新前操作
     *
     * @method onBeforeUpdate
     *
     * @param [type] $data [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-19T11:16:58+080
     */
    public static function onBeforeUpdate($data)
    {
    }

    /**
     * @xk 写入前操作
     *
     * @method onBeforeWrite
     *
     * @param [type] $data [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-19T11:17:11+080
     */
    public static function onBeforeWrite($data)
    {

        unset($data['act']);
        unset($data['controllers']);
        unset($data['actions']);
        if (!$data->create_time) {
            $data->update_time = $data->create_time = date('Y-m-d H:i:s');
        }
        $data->update_time = date('Y-m-d H:i:s'); // 操作时间 实际发布时间
        // 获取父级族谱
        $data->parent_str = self::checkParent($data['parent_id'], 'parent_str', 'menu_id');
        if ('' == $data->sign && $data->menu_name) {
            $data->sign = getPinyin($data->menu_name);
        }
        // 菜单排序id
        if ($data->order_id <= 0) {
            $data->order_id = self::max('order_id') + 1;
        }
        $data['linkurl'] = $data['menu_controller'];
        if ($data['menu_action']) {
            $data['linkurl'] = $data['menu_controller'].'/'.$data['menu_action'];
        }
    }
    /**
     * @xk 写入后操作
     * 写入后 更新对应的族谱信息
     *
     * @method onAfterWrite
     *
     * @param [type] $data [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-19T11:24:39+080
     */
    public static function onAfterWrite($data)
    {
        self::updateParentStr($data['menu_id'], $data['parent_str'], 'parent_id','menu_id'); //写入后置操作 更新族谱
    }

    /**
     * @xk 删除后操作
     * 删除后 同步删除对应的子菜单
     *
     * @method onAfterDelete
     *
     * @param [type] $data [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-19T11:24:05+080
     */
    public static function onAfterDelete($data)
    {
        self::destoryChildren($data['menu_id'], 'menu_id');
    }
}
